/** 加法 */
export function add(nums: number[]) {
  const ints = getInts(nums);
  const {nums: floats, len: maxLength} = getFloats(nums);

  let intSum = ints.reduce((a, b) => a + b, 0);
  let floatSum = floats.reduce((a, b) => a + b, 0);
  const sFloatSum = String(floatSum);

  floatSum = Number(sFloatSum.slice(sFloatSum.length - maxLength));
  intSum = intSum + Number(sFloatSum.slice(0, sFloatSum.length - maxLength));

  return Number(`${intSum}.${floatSum}`);
}
/** 减法 */
export function sub(nums: number[]) {
  const ints = getInts(nums);
  const {nums: floats, len: maxLength} = getFloats(nums);

  let intDiff = ints.reduce((a, b) => a - b, 0);
  let floatDiff = floats.reduce((a, b) => a - b, 0);
  const sFloatDiff = String(floatDiff);
  if (sFloatDiff.length > maxLength) {
    floatDiff = Number(sFloatDiff.slice(sFloatDiff.length - maxLength));
    intDiff = intDiff - Number(sFloatDiff.slice(0, sFloatDiff.length - maxLength));
  } else {
    floatDiff = Number(sFloatDiff);
  }
  return Number(`${intDiff}.${floatDiff}`);
}
/** 获取整数部分 */
export function getInts(nums: number[]) {
  return nums.map(n => Math.floor(n));
}
/** 获取小数部分 */
export function getFloats(nums: number[]) {
  let sFloats = nums.map(n => String(n).split('.')[1] || '0');
  const maxLength = Math.max(...sFloats.map(s => s.length));
  sFloats = sFloats.map(s => s.padEnd(maxLength, '0'));
  return {nums: sFloats.map(s => Number(s)), len: maxLength};
}
